package oa.hleast.xswl.service.appsicon.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.constant.ConfigKeyConstant;
import oa.hleast.common.redis.component.template.RedisRepository;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.constant.CommonConstant;
import oa.hleast.xswl.mapper.appsicon.AppsIconMapper;
import oa.hleast.xswl.pojo.domain.SysDictItem;
import oa.hleast.xswl.pojo.domain.appsicon.CmsAppsIcon;
import oa.hleast.xswl.pojo.dto.DictItemValueDto;
import oa.hleast.xswl.pojo.form.appsicon.CmsAppsIconForm;
import oa.hleast.xswl.pojo.vo.appsicon.CmsAppsIconVo;
import oa.hleast.xswl.pojo.vo.appsicon.HomePageAppsIconVo;
import oa.hleast.xswl.service.ISysCommonConfigService;
import oa.hleast.xswl.service.ISysDictItemService;
import oa.hleast.xswl.service.ISysDictService;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.appsicon.IAppsIconService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 302
 * @description
 * @date 2021/3/12
 */

@Transactional
@Slf4j
@Service
@AllArgsConstructor
public class AppsIconServiceImpl extends ServiceImpl<AppsIconMapper, CmsAppsIcon> implements IAppsIconService {

    private final RedisRepository redisRepository;
    private final IUmsUserService umsUserService;
    private final ISysDictService dictService;
    private final ISysCommonConfigService commonConfigService;
    private final ISysDictItemService dictItemService;

    @Override
    public Page<HomePageAppsIconVo> getHomePageIcon(Integer page, Integer limit, Long dictItemAppsIconTypeId) {

        //查询数据库
        Page<CmsAppsIconVo> cmsAppsIconVoPage = this.pageList(page, limit, CommonConstant.HOME_PAGE_ICON_DISPLAY, dictItemAppsIconTypeId);

        Page<HomePageAppsIconVo> homePageVoPage = new Page<>(cmsAppsIconVoPage.getSize(), cmsAppsIconVoPage.getTotal());
        if (cmsAppsIconVoPage.getTotal() != 0) {
            List<HomePageAppsIconVo> homePageVoList = conversionToHomePageVoList(cmsAppsIconVoPage.getRecords());
            homePageVoPage.setRecords(homePageVoList);
        }

        return homePageVoPage;
    }

    @Override
    public List<SysDictItem> findAppsIconType() {

        List<SysDictItem> dictItemListResult = null;
        try {
            String configKey = commonConfigService.getByConfigKey(ConfigKeyConstant.APPS_ICON_KEY).getValue();
            dictItemListResult = dictItemService.getByDictByCode(configKey);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("e ={}", e, e.getMessage());
            throw new BizException("系统异常，请联系管理员");
        }

        if (ObjectUtils.isEmpty(dictItemListResult)) {
            log.info("没有找到 应用图标位置的字典项值 code= {} ,dictItemListResult = {}", ConfigKeyConstant.APPS_ICON_KEY, dictItemListResult);
            return new ArrayList<>();
        }
        return dictItemListResult;
    }

    @Override
    public Page<CmsAppsIconVo> pageListByApps(Integer page, Integer limit, String appTypeKey) {
        log.info("appTypeKey = {}", appTypeKey);
        Long dictItemAppsIconTypeId = new Long(commonConfigService.getByConfigKey(appTypeKey).getValue());

        log.info("appTypeKey = {},dictItemAppsIconTypeId = {}", appTypeKey, dictItemAppsIconTypeId);
        return this.pageList(page, limit, null, dictItemAppsIconTypeId);
    }

    @Override
    public Page<HomePageAppsIconVo> getAppHomePageIcon(Integer page, Integer limit, Long dictItemAppsIconTypeId) {
        //查询数据库
        Page<CmsAppsIconVo> cmsAppsIconVoPage = this.pageList(page, limit, CommonConstant.HOME_PAGE_ICON_DISPLAY, dictItemAppsIconTypeId);

        Page<HomePageAppsIconVo> homePageVoPage = new Page<>(cmsAppsIconVoPage.getSize(), cmsAppsIconVoPage.getTotal());
        if (cmsAppsIconVoPage.getTotal() != 0) {
            List<HomePageAppsIconVo> homePageVoList = conversionToHomePageVoList(cmsAppsIconVoPage.getRecords());
            homePageVoPage.setRecords(homePageVoList);
        }

        return homePageVoPage;
    }


    /**
     * =========  基础功能 CRUD =========
     */
    @Override
    public boolean add(CmsAppsIconForm cmsAppsIconForm) {


        CmsAppsIcon cmsAppsIcon = conversionToPojo(cmsAppsIconForm);
        Long dictItemAppsIconTypeId = new Long(Objects.requireNonNull(commonConfigService.getByConfigKey(cmsAppsIconForm.getAppTypeKey()).getValue()));
//        dictItemAppsIconTypeId = new Long(userFeignService.getByConfigKey(anEnum.getKey()).getData().getValue());

        cmsAppsIcon.setDictItemAppsIconTypeId(dictItemAppsIconTypeId);

        return this.save(cmsAppsIcon);
    }

    @Override
    public Page<CmsAppsIconVo> pageList(Integer page, Integer limit, Integer display, Long dictItemAppsIconTypeId) {

        LambdaQueryWrapper<CmsAppsIcon> queryWrapper = new LambdaQueryWrapper<CmsAppsIcon>()
                .orderByAsc(CmsAppsIcon::getSort)
                .orderByDesc(CmsAppsIcon::getGmtModified)
                .orderByDesc(CmsAppsIcon::getGmtCreate);

        if (ObjectUtils.isNotEmpty(dictItemAppsIconTypeId)) {
            queryWrapper.eq(CmsAppsIcon::getDictItemAppsIconTypeId, dictItemAppsIconTypeId);
        }

        if (ObjectUtils.isNotEmpty(display)) {
            queryWrapper.eq(CmsAppsIcon::getDisplay, display);
        }

        Page<CmsAppsIcon> cmsAppsIconPage = this.page(new Page<>(page, limit), queryWrapper);

        if (cmsAppsIconPage.getTotal() == 0) {
            return new Page<>(page, limit);
        }

        List<CmsAppsIconVo> cmsAppsIconList = cmsAppsIconPage.getRecords().stream().map(this::conversionToVo).collect(Collectors.toList());

        Page<CmsAppsIconVo> cmsAppsIconVoPage = new Page<>(page, limit, cmsAppsIconPage.getTotal());
        cmsAppsIconVoPage.setRecords(cmsAppsIconList);


        return cmsAppsIconVoPage;
    }

    @Override
    public boolean update(CmsAppsIconForm articleForm) {

        //校验ID
        CmsAppsIcon cmsAppsIconOld = this.getById(articleForm.getId());
        if (ObjectUtils.isEmpty(cmsAppsIconOld)) {
            throw new BizException("没有找到该数据！");
        }

        CmsAppsIcon cmsAppsIcon = new CmsAppsIcon();
        BeanUtil.copyProperties(articleForm, cmsAppsIcon);
        cmsAppsIcon.setId(cmsAppsIconOld.getId());


        cmsAppsIcon.setGmtModified(new Date());

        return this.updateById(cmsAppsIcon);
    }

    @Override
    public boolean deleteByIds(List<String> asList) {
        if (CollectionUtils.isFull(asList)) {
            throw new BizException("没有选择任何数据，请重新操作！");
        }

        return this.removeByIds(asList);
    }

    @Override
    public CmsAppsIconVo detail(Integer id) {

        CmsAppsIcon CmsAppsIcon = this.getById(id);

        CmsAppsIconVo cmsAppsIconVo = conversionToVo(CmsAppsIcon);

        return cmsAppsIconVo;
    }

    /**
     * ===========  私有方法 ===========
     */
    private CmsAppsIconVo conversionToVo(CmsAppsIcon cmsAppsIcon) {
        if (ObjectUtils.isEmpty(cmsAppsIcon)) {
            return new CmsAppsIconVo();
        }

        CmsAppsIconVo cmsAppsIconVo = new CmsAppsIconVo();
        BeanUtil.copyProperties(cmsAppsIcon, cmsAppsIconVo);

        return cmsAppsIconVo;
    }

    private CmsAppsIcon conversionToPojo(CmsAppsIconForm appsIconForm) {
        if (ObjectUtils.isEmpty(appsIconForm)) {
            throw new BizException("提交的数据表单为空");
        }

        CmsAppsIcon cmsAppsIcon = new CmsAppsIcon();
        BeanUtil.copyProperties(appsIconForm, cmsAppsIcon);

        return cmsAppsIcon;
    }


    private List<HomePageAppsIconVo> conversionToHomePageVoList(List<CmsAppsIconVo> cmsAppsIconVoList) {
        if (CollectionUtils.isEmpty(cmsAppsIconVoList)) {
            return new ArrayList<>();
        }
        return cmsAppsIconVoList.stream().map(this::conversionToHomePageVo).collect(Collectors.toList());
    }

    private HomePageAppsIconVo conversionToHomePageVo(CmsAppsIconVo cmsAppsIconVo) {
        HomePageAppsIconVo homePageWebsiteBannerVo = new HomePageAppsIconVo();
        BeanUtil.copyProperties(cmsAppsIconVo, homePageWebsiteBannerVo);
        //转换应用图标位置名称
        List<SysDictItem> dictItemValueDtoList = findAppsIconType();
        for (SysDictItem dictItemValueDto : dictItemValueDtoList) {

            if (dictItemValueDto.getId().toString().equals(cmsAppsIconVo.getDictItemAppsIconTypeId())) {
                homePageWebsiteBannerVo.setAppsIconTypeName(dictItemValueDto.getName());
            }
        }
        return homePageWebsiteBannerVo;
    }

    private HomePageAppsIconVo conversionPojoToHomePageVo(CmsAppsIcon cmsAppsIcon) {
        HomePageAppsIconVo homePageWebsiteBannerVo = new HomePageAppsIconVo();
        BeanUtil.copyProperties(cmsAppsIcon, homePageWebsiteBannerVo);
        //转换应用图标位置名称
        List<SysDictItem> dictItemValueDtoList = findAppsIconType();
        return homePageWebsiteBannerVo;
    }


}
